package com.dianban.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.dianban.base.ResultCode;
import com.dianban.base.ResultVo;
import com.dianban.config.redis.RedisUtil;
import com.dianban.dao.UserDao;
import com.dianban.entity.*;
import com.dianban.service.*;
import com.dianban.utils.TencentSmsUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户业务实现层
 */
@Service
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private UserCouponLogService userCouponLogService;
    @Autowired
    private UserEnergyLogService userEnergyLogService;
    @Autowired
    private ShopConsumeLogService shopConsumeLogService;
    @Autowired
    private UserMoneyLogService userMoneyLogService;
    @Autowired
    private UserGoldBeanLogService userGoldBeanLogService;
    @Autowired
    private ShoproOrderService shoproOrderService;
    @Autowired
    private FacePayStoreOrderService facePayStoreOrderService;
    @Autowired
    private FacePayStoreConsumeLogService facePayStoreConsumeLogService;
    @Autowired
    private FacePayCommissionLogService facePayCommissionLogService;
    @Autowired
    private FacePayCommissionVerifyLogService facePayCommissionVerifyLogService;
    @Autowired
    private UserCommissionLogService userCommissionLogService;

    @Override
    public User findByMobile(String phone) {
        User user = userDao.findByMobile(phone);
        return user;
    }

    @Override
    public User findById(Long id) {
        return userDao.findById(id);
    }

    @Override
    public List<User> findChildren(Long id) {
        return userDao.findChildren(id);
    }

    @Override
    @Transactional(readOnly = false)
    public ResultVo updateCoupon(BigDecimal coupon, BigDecimal energy, Long userId, Long shopUserId) {
        Map<String, Object> resultMap = new HashMap<>();
        if (coupon == null) {
            return ResultVo.error(ResultCode.ERROR, "支付的福利券不能为空");
        }
        if (energy == null) {
            return ResultVo.error(ResultCode.ERROR, "支付的积分不能为空");
        }
        if (userId == null) {
            return ResultVo.error(ResultCode.ERROR, "用户ID不能为空");
        }
        String key = "updateCouponByUserId:" + userId;
        try {
            if (RedisUtil.hasKey(key)) {
                return ResultVo.error(ResultCode.FAIL);
            }
            RedisUtil.set(key, 1);
            User user = findById(userId);
            if (user == null) {
                return ResultVo.error(ResultCode.ERROR, "用户不能为空");
            }
            BigDecimal curCoupon = user.getCoupon();
            BigDecimal curEnergy = user.getEnergy();
            if (curCoupon == null) {
                return ResultVo.error(ResultCode.ERROR, "用户福利券为空");
            }
            if (coupon.add(curCoupon).compareTo(BigDecimal.ZERO) < 0) {
                return ResultVo.error(ResultCode.ERROR420);
            }
            if (curEnergy == null) {
                return ResultVo.error(ResultCode.ERROR421);
            }
            if (energy.add(curCoupon).compareTo(BigDecimal.ZERO) < 0) {
                return ResultVo.error(ResultCode.ERROR421);
            }
            int count = userDao.updateCoupon(coupon, energy, userId);
            if (count != 1) {
                RedisUtil.del(key);
                return ResultVo.error(ResultCode.ERROR420);
            }
            UserCouponLog userCouponLog = new UserCouponLog();
            userCouponLog.setUserId(userId);
            userCouponLog.setCoupon(coupon);
            userCouponLog.setBefore(curCoupon);
            userCouponLog.setAfter(coupon.add(curCoupon));
            userCouponLog.setWalletType("coupon");
            userCouponLog.setType("buy");
            userCouponLog.setMemo("购买产品");
            userCouponLog.setOperator(userId);
            userCouponLog.setExt("");
            userCouponLog.setRelationId(0L);
            userCouponLogService.insert(userCouponLog);
            // 支付福利券金额
            resultMap.put("paymentCoupon", coupon.negate());
            // 福利券余额
            resultMap.put("balanceCoupon", userCouponLog.getAfter());
            UserEnergyLog userEnergyLog = new UserEnergyLog();
            userEnergyLog.setUserId(userId);
            userEnergyLog.setEnergy(energy);
            userEnergyLog.setBefore(curEnergy);
            userEnergyLog.setAfter(energy.add(curEnergy));
            userEnergyLog.setType("buy");
            userEnergyLog.setExt("");
            userEnergyLog.setMemo("购买产品");
            userEnergyLog.setOperator(userId);
            userEnergyLog.setAssignmentId(0L);
            userEnergyLogService.insert(userEnergyLog);
            // 支付的积分
            resultMap.put("paymentEnergy", energy.negate());
            // 积分余额
            resultMap.put("balanceEnergy", userEnergyLog.getAfter());
            ShopConsumeLog shopConsumeLog = new ShopConsumeLog();
            shopConsumeLog.setShopUserId(shopUserId);
            shopConsumeLog.setUserId(userId);
            shopConsumeLog.setCoupon(coupon);
            shopConsumeLog.setEnergy(energy);
            shopConsumeLog.setMemo("福利券消费");
            shopConsumeLogService.insert(shopConsumeLog);
        } catch (Exception e) {
            return ResultVo.error(ResultCode.FAIL);
        } finally {
            RedisUtil.del(key);
            return ResultVo.success(resultMap, "支付成功");
        }

    }

    @Override
    @Transactional(readOnly = false)
    public ResultVo updateMoney(BigDecimal money, Long userId, Long shopUserId) {
        Map<String, Object> resultMap = new HashMap<>();
        if (money == null) {
            return ResultVo.error(ResultCode.ERROR, "支付的余额不能为空");
        }
        if (userId == null) {
            return ResultVo.error(ResultCode.ERROR, "用户ID不能为空");
        }
        String key = "updateMoneyByUserId:" + userId;
        try {
            if (RedisUtil.hasKey(key)) {
                return ResultVo.error(ResultCode.FAIL);
            }
            RedisUtil.set(key, 1);
            User user = findById(userId);
            if (user == null) {
                return ResultVo.error(ResultCode.ERROR, "用户不能为空");
            }
            BigDecimal curMoney = user.getMoney();
            if (curMoney == null) {
                return ResultVo.error(ResultCode.ERROR, "用户余额为空");
            }
            if (money.add(curMoney).compareTo(BigDecimal.ZERO) < 0) {
                return ResultVo.error(ResultCode.ERROR422);
            }
            int count = userDao.updateMoney(money, userId);
            if (count != 1) {
                RedisUtil.del(key);
                return ResultVo.error(ResultCode.ERROR420);
            }
            UserMoneyLog userMoneyLog = new UserMoneyLog();
            userMoneyLog.setUserId(userId);
            userMoneyLog.setMoney(money);
            userMoneyLog.setBefore(curMoney);
            userMoneyLog.setAfter(money.add(curMoney));
            userMoneyLog.setType("buy");
            userMoneyLog.setMemo("购买产品");
            userMoneyLog.setOperator(userId);
            userMoneyLog.setExt("");
            userMoneyLogService.insert(userMoneyLog);
            resultMap.put("paymentMoney", money.negate());
            resultMap.put("balanceMoney", userMoneyLog.getAfter());
            ShopConsumeLog shopConsumeLog = new ShopConsumeLog();
            shopConsumeLog.setShopUserId(shopUserId);
            shopConsumeLog.setUserId(userId);
            shopConsumeLog.setMoney(money);
            shopConsumeLog.setMemo("余额消费");
            shopConsumeLogService.insert(shopConsumeLog);
        } catch (Exception e) {
            return ResultVo.error(ResultCode.FAIL);
        } finally {
            RedisUtil.del(key);
            return ResultVo.success(resultMap, "支付成功");
        }
    }

    @Override
    @Transactional(readOnly = false)
    public ResultVo updateGoldBean(BigDecimal goldBean, Long userId, Long shopUserId) {
        Map<String, Object> resultMap = new HashMap<>();
        if (goldBean == null) {
            return ResultVo.error(ResultCode.ERROR, "支付的金豆不能为空");
        }
        if (userId == null) {
            return ResultVo.error(ResultCode.ERROR, "用户ID不能为空");
        }
        String key = "updateGoldBeanByUserId:" + userId;
        try {
            if (RedisUtil.hasKey(key)) {
                return ResultVo.error(ResultCode.FAIL);
            }
            RedisUtil.set(key, 1);
            User user = findById(userId);
            if (user == null) {
                return ResultVo.error(ResultCode.ERROR, "用户不能为空");
            }
            BigDecimal curGoldBean = user.getGoldBean();
            if (curGoldBean == null) {
                return ResultVo.error(ResultCode.ERROR, "用户金豆为空");
            }
            if (goldBean.add(curGoldBean).compareTo(BigDecimal.ZERO) < 0) {
                return ResultVo.error(ResultCode.ERROR423);
            }
            int count = userDao.updateGoldBean(goldBean, userId);
            if (count != 1) {
                RedisUtil.del(key);
                return ResultVo.error(ResultCode.ERROR420);
            }
            UserGoldBeanLog userGoldBeanLog = new UserGoldBeanLog();
            userGoldBeanLog.setUserId(userId);
            userGoldBeanLog.setGoldBean(goldBean);
            userGoldBeanLog.setBefore(curGoldBean);
            userGoldBeanLog.setAfter(curGoldBean.add(goldBean));
            userGoldBeanLog.setType("buy");
            userGoldBeanLog.setMemo("购买产品");
            userGoldBeanLog.setOperator(userId);
            userGoldBeanLog.setExt("");
            userGoldBeanLogService.insert(userGoldBeanLog);
            resultMap.put("paymentGoldBean", goldBean.negate());
            resultMap.put("balanceGoldBean", userGoldBeanLog.getAfter());
            ShopConsumeLog shopConsumeLog = new ShopConsumeLog();
            shopConsumeLog.setShopUserId(shopUserId);
            shopConsumeLog.setUserId(userId);
            shopConsumeLog.setGoldBean(goldBean);
            shopConsumeLog.setMemo("金豆消费");
            shopConsumeLogService.insert(shopConsumeLog);
        } catch (Exception e) {
            return ResultVo.error(ResultCode.FAIL);
        } finally {
            RedisUtil.del(key);
            return ResultVo.success(resultMap, "支付成功");
        }
    }

    @Override
    public Map<String, Object> findVerify(String type, User user, Long goodsId, Long storeId, String orderType, String money) {
        // type 10 线上套餐核销/20 线下套餐核销 /30 带新体验核销/ 40 非会员首次核销
        // orderType  10线上/20 线下
        Long userId = user.getId();
        Map<String, Object> result = new HashMap<>();
        result.put("userId", userId);
        result.put("goodsId", goodsId);
        result.put("type", type);
        result.put("orderType", orderType);
        List<Long> orderIdList = new ArrayList<>();
        Integer verifyCount = 0;
        if ("10".equals(type)) {
            List<ShoproOrder> shoproOrderList = shoproOrderService.findVerifyList(userId, goodsId, null);
            for (ShoproOrder shoproOrder : shoproOrderList) {
                Integer remainVerifyCount = shoproOrder.getCanVerifyCount() - shoproOrder.getHasVerifyCount();
                if (remainVerifyCount > 0) {
                    verifyCount = verifyCount + remainVerifyCount;
                    orderIdList.add(shoproOrder.getId());
                }
            }

        } else if ("20".equals(type)) {
            List<FacePayStoreOrder> facePayStoreOrderList = facePayStoreOrderService.findVerifyList(userId, storeId, money,20,null);
            for (FacePayStoreOrder facePayStoreOrder : facePayStoreOrderList) {
                Integer remainVerifyCount = facePayStoreOrder.getCanVerifyCount() - facePayStoreOrder.getHasVerifyCount();
                if (remainVerifyCount > 0) {
                    verifyCount = verifyCount + remainVerifyCount;
                    orderIdList.add(facePayStoreOrder.getId());
                }
            }
        } else if ("30".equals(type)) {
            List<FacePayStoreConsumeLog> facePayStoreConsumeLogList = facePayStoreConsumeLogService.findList(userId, null, "30");
            if (facePayStoreConsumeLogList.isEmpty()) {
                Long pid = user.getPid();
                Long higherId = user.getHigherId();
                if (StringUtils.isBlank(orderType) || "10".equals(orderType)) {
                    List<ShoproOrder> shoproOrderList = shoproOrderService.findVerifyList(pid, goodsId, null);
                    for (ShoproOrder shoproOrder : shoproOrderList) {
                        Integer remainVerifyCount = shoproOrder.getTakenewVerifyCount() - shoproOrder.getTakenewHasVerifyCount();
                        if (remainVerifyCount > 0) {
                            verifyCount = 1;
                            orderIdList.add(shoproOrder.getId());
                            result.put("order_type", 1);
                            break;
                        }
                    }
                    if (verifyCount == 0) {
                        shoproOrderList = shoproOrderService.findVerifyList(higherId, goodsId, null);
                        for (ShoproOrder shoproOrder : shoproOrderList) {
                            Integer remainVerifyCount = shoproOrder.getTakenewVerifyCount() - shoproOrder.getTakenewHasVerifyCount();
                            if (remainVerifyCount > 0) {
                                verifyCount = 1;
                                orderIdList.add(shoproOrder.getId());
                                result.put("order_type", 1);
                                break;
                            }
                        }
                    }
                } else if (StringUtils.isBlank(orderType) || "20".equals(orderType)) {
                    if (verifyCount == 0) {
                        List<FacePayStoreOrder> facePayStoreOrderList = facePayStoreOrderService.findVerifyList(pid, storeId, money,20,null);
                        for (FacePayStoreOrder facePayStoreOrder : facePayStoreOrderList) {
                            Integer remainVerifyCount = facePayStoreOrder.getCanVerifyCount() - facePayStoreOrder.getHasVerifyCount();
                            if (remainVerifyCount > 0) {
                                verifyCount = 1;
                                orderIdList.add(facePayStoreOrder.getId());
                                result.put("order_type", 2);
                                break;
                            }
                        }
                    }
                    if (verifyCount == 0) {
                        List<FacePayStoreOrder> facePayStoreOrderList = facePayStoreOrderService.findVerifyList(higherId, storeId, money,20,null);
                        for (FacePayStoreOrder facePayStoreOrder : facePayStoreOrderList) {
                            Integer remainVerifyCount = facePayStoreOrder.getCanVerifyCount() - facePayStoreOrder.getHasVerifyCount();
                            if (remainVerifyCount > 0) {
                                verifyCount = 1;
                                orderIdList.add(facePayStoreOrder.getId());
                                result.put("order_type", 2);
                                break;
                            }
                        }
                    }
                }
            }
        } else if ("40".equals(type)) {
            //if(user.getIsVip().equals(0)){
            List<FacePayStoreConsumeLog> facePayStoreConsumeLogList = facePayStoreConsumeLogService.findList(userId, null, "40");
            if (facePayStoreConsumeLogList.isEmpty()) {
                List<FacePayStoreOrder> facePayStoreOrderList = facePayStoreOrderService.findVerifyList(userId, storeId, money,20,null);
                if (facePayStoreOrderList.isEmpty()) {
                    verifyCount = 2;
                    result.put("canBuy", "1");
                } else {
                    for (FacePayStoreOrder facePayStoreOrder : facePayStoreOrderList) {
                        Integer remainVerifyCount = facePayStoreOrder.getCanVerifyCount() - facePayStoreOrder.getHasVerifyCount();
                        if (remainVerifyCount > 0) {
                            verifyCount = verifyCount + remainVerifyCount;
                            orderIdList.add(facePayStoreOrder.getId());
                        }
                    }
                }

            }
            //}
        }
        result.put("orderIdList", orderIdList);
        result.put("verifyCount", verifyCount);
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public int updateFacePayCommission(BigDecimal facePayCommission, Long userId, String type, Long orderId, String ext) {
        User user = findById(userId);
        if (user == null) {
            return 0;
        }
        FacePayCommissionLog facePayCommissionLog = new FacePayCommissionLog();
        facePayCommissionLog.setUserId(userId);
        facePayCommissionLog.setMoney(facePayCommission);
        facePayCommissionLog.setBefore(user.getFacePayCommission());
        facePayCommissionLog.setAfter(facePayCommission.add(user.getFacePayCommission()));
        facePayCommissionLog.setType(type);
        facePayCommissionLog.setExt(ext);
        facePayCommissionLog.setOperator(userId);
        if ("10".equals(type)) {
            facePayCommissionLog.setMemo("线上订单线下核销清分");
            facePayCommissionLog.setOnlineOrderId(orderId);
        } else if ("20".equals(type)) {
            facePayCommissionLog.setMemo("线下订单付款成功清分");
            facePayCommissionLog.setOfflineOrderId(orderId);
        } else if ("40".equals(type)) {
            facePayCommissionLog.setMemo("线下订单付款成功清分");
            facePayCommissionLog.setOfflineOrderId(orderId);
        }
        if (ext.equals("美导顾客佣金") || ext.equals("亚健康护理美导师（卖手）") || ext.equals("门店老师")) {
            FacePayCommissionVerifyLog facePayCommissionVerifyLog = new FacePayCommissionVerifyLog();
            BeanUtils.copyProperties(facePayCommissionLog, facePayCommissionVerifyLog);
            if (ext.equals("亚健康护理美导师（卖手）") || ext.equals("门店老师")) {
                User guideUser = userDao.findById(userId);
                if (StringUtils.isNotBlank(guideUser.getMobile())) {
                    String code = String.valueOf((int) (Math.random() * 9000 + 1000));
                    TencentSmsUtils.sendSms("5", guideUser.getMobile(), code, String.valueOf(orderId));
                    facePayCommissionVerifyLog.setCode(code);
                }
            }
            return facePayCommissionVerifyLogService.insert(facePayCommissionVerifyLog);
        }
        int count = userDao.updateFacePayCommission(facePayCommission, userId);
        if (count > 0) {
            facePayCommissionLogService.insert(facePayCommissionLog);
        }
        return count;
    }

    @Override
    public int canBecomeStation(Long userId, Long storeId) {
        int isBuy = buyOrder5000(userId, storeId);
        if (isBuy == 0) {
            return 0;
        }
        List<User> userList = userDao.findChildren(userId);
        int orderNum = 0;
        for (User user : userList) {
            isBuy = buyOrder5000(user.getId(), storeId);
            if (isBuy == 1) {
                orderNum = orderNum + 1;
            }
            if (orderNum >= 10) {
                return 1;
            }
        }
        return 0;
    }

    private int buyOrder5000(Long userId, Long storeId) {
        List<ShoproOrder> shoproOrderList = shoproOrderService.findVerifyList(userId, ShoproOrder.goodsId_5000, storeId);
        if (shoproOrderList.isEmpty()) {
            List<FacePayStoreOrder> facePayStoreOrderList = facePayStoreOrderService.findVerifyList(userId, storeId, "5000",20,null);
            if (facePayStoreOrderList.isEmpty()) {
                return 0;
            }
            boolean buy5000 = false;
            for (FacePayStoreOrder facePayStoreOrder : facePayStoreOrderList) {
                if (facePayStoreOrder.getMoney().compareTo(new BigDecimal("5000")) == 0) {
                    buy5000 = true;
                    break;
                }
            }
            if (!buy5000) {
                return 0;
            }
        }
        return 1;
    }

    @Override
    @Transactional(readOnly = false)
    public int updateFacePayCommission(BigDecimal facePayCommission, Long userId, String type, Long orderId, String ext, FacePayCommissionConfig facePayCommissionConfig) {
        User user = findById(userId);
        if (user == null) {
            return 0;
        }
        BigDecimal parentFacePayCommission = BigDecimal.ZERO;
        // 判断是不是门店老板,不打折,否则只能得到90%,其他10%奖励给直推
        if (StringUtils.isNotBlank(ext) && ("门店老板".equals(ext) || "新康美老板".equals(ext) || "店老板".equals(ext))) {
            // TODO: 不扣除门店老板的,直接奖励门店推荐人,直接做到配置刷进去
        } else {
            parentFacePayCommission = facePayCommission.multiply(new BigDecimal(10)).divide(new BigDecimal(100), 3, RoundingMode.DOWN);
            facePayCommission = facePayCommission.multiply(new BigDecimal(90)).divide(new BigDecimal(100), 3, RoundingMode.DOWN);
        }
        FacePayCommissionLog facePayCommissionLog = new FacePayCommissionLog();
        facePayCommissionLog.setUserId(userId);
        facePayCommissionLog.setMoney(facePayCommission);
        facePayCommissionLog.setBefore(user.getFacePayCommission());
        facePayCommissionLog.setAfter(facePayCommission.add(user.getFacePayCommission()));
        facePayCommissionLog.setType(type);
        facePayCommissionLog.setExt(ext);
        facePayCommissionLog.setOperator(userId);
        if ("10".equals(type)) {
            facePayCommissionLog.setMemo("线上订单线下核销清分");
            facePayCommissionLog.setOnlineOrderId(orderId);
        } else if ("20".equals(type)) {
            facePayCommissionLog.setMemo("线下订单付款成功清分");
            facePayCommissionLog.setOfflineOrderId(orderId);
        } else if ("40".equals(type)) {
            facePayCommissionLog.setMemo("线下订单付款成功清分");
            facePayCommissionLog.setOfflineOrderId(orderId);
        }
        if (facePayCommissionConfig != null && facePayCommissionConfig.getIsVerify() != null && facePayCommissionConfig.getIsVerify().equals(1)) {
            FacePayCommissionVerifyLog facePayCommissionVerifyLog = new FacePayCommissionVerifyLog();
            BeanUtils.copyProperties(facePayCommissionLog, facePayCommissionVerifyLog);
            if (facePayCommissionConfig.getIsSms() != null && facePayCommissionConfig.getIsSms().equals(1)) {
                User guideUser = userDao.findById(userId);
                if (StringUtils.isNotBlank(guideUser.getMobile())) {
                    String code = String.valueOf((int) (Math.random() * 9000 + 1000));
                    TencentSmsUtils.sendSms("5", guideUser.getMobile(), code, String.valueOf(orderId));
                    facePayCommissionVerifyLog.setCode(code);
                }
            }
            return facePayCommissionVerifyLogService.insert(facePayCommissionVerifyLog);
        }
        int count = userDao.updateFacePayCommission(facePayCommission, userId);
        if (count > 0) {
            facePayCommissionLogService.insert(facePayCommissionLog);
        }
        try {
            if (parentFacePayCommission.compareTo(BigDecimal.ZERO) > 0) {
                // 获取直接推荐人
                User user1 = userDao.findById(userId);
                if (user1 != null) {
                    User puser = userDao.findById(user1.getPid());
                    if (puser != null) {
                        facePayCommissionLog = new FacePayCommissionLog();
                        facePayCommissionLog.setUserId(puser.getId());
                        facePayCommissionLog.setMoney(parentFacePayCommission);
                        facePayCommissionLog.setBefore(puser.getFacePayCommission());
                        facePayCommissionLog.setAfter(parentFacePayCommission.add(puser.getFacePayCommission()));
                        facePayCommissionLog.setType(type);
                        facePayCommissionLog.setExt(ext + "-" + "直推用户" + user1.getMobile() + "-10%奖励");
                        facePayCommissionLog.setOperator(puser.getId());
                        if ("10".equals(type)) {
                            facePayCommissionLog.setMemo("线上订单线下核销清分");
                            facePayCommissionLog.setOnlineOrderId(orderId);
                        } else if ("20".equals(type)) {
                            facePayCommissionLog.setMemo("线下订单付款成功清分");
                            facePayCommissionLog.setOfflineOrderId(orderId);
                        } else if ("40".equals(type)) {
                            facePayCommissionLog.setMemo("线下订单付款成功清分");
                            facePayCommissionLog.setOfflineOrderId(orderId);
                        }
                        int pcount = userDao.updateFacePayCommission(facePayCommission, puser.getId());
                        if (pcount > 0) {
                            facePayCommissionLogService.insert(facePayCommissionLog);
                        }
                    }
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    @Transactional(readOnly = false)
    public void updateFacePayCommissionAndMoneyLog(BigDecimal money, User user) {
        int count = userDao.subFacePayCommission(money, user.getId());
        if (count > 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("transfer_id", user.getId());
            UserCommissionLog userCommissionLog = new UserCommissionLog();
            userCommissionLog.setUserId(user.getId());
            userCommissionLog.setMoney(money.negate());
            userCommissionLog.setBefore(user.getFacePayCommission());
            userCommissionLog.setAfter(user.getFacePayCommission().subtract(money));
            userCommissionLog.setType("facepaycommission");
            userCommissionLog.setMemo("刷脸设备佣金提现");
            userCommissionLog.setExt(JSONObject.toJSONString(map));
            userCommissionLog.setOperator(0L);
            userCommissionLogService.insert(userCommissionLog);
        }
    }

    @Override
    @Transactional(readOnly = false)
    public void updateOpenid(String openid, String userId) {
        userDao.updateOpenid(openid, userId);
    }

    @Override
    public List<User> findFirstChildren(Long id) {
        return userDao.findFirstChildren(id);
    }

}
